﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Grepper
{
    public class Grepper
    {
        static void Main(string[] args)
        {
            var ms = DefaultForbidden("test.fs");
            Console.WriteLine(string.Format("found {0} matches:", ms.Count));
            foreach (var m in ms)
            {
                Console.WriteLine(m);
            }

            Console.ReadKey();
        }

        public static List<Tuple<string, int>> Search(Regex[] regexps, string file)
        {
            var rows = File.ReadAllLines(file);
            var res = new List<Tuple<string, int>>();

            for (int i = 0; i < rows.Length; i++)
            {
                var row = rows[i]; 
                foreach (var regex in regexps)
                {
                    var matches = regex.Match(row).Captures;
                    if (matches.Count > 0)
                    {
                        foreach (Match match in matches)
                        {
                            if (!match.Value.StartsWith(".net"))
                            {
                                res.Add(new Tuple<string, int>(match.Value, i));
                            }
                        }
                    }
                }
            }
            return res;
        }

        public static List<Tuple<string, int>> DefaultForbidden(string file)
        {
            var mutable = new Regex(@"mutable");
            var unit = new Regex(@"\(\)");
            var assign_m = new Regex(@"\<\-");
            var assign_r = new Regex(@"\:\=");
            //var do_ = new Regex(@" do ");
            var while_ = new Regex(@"while");
            var for_ = new Regex(@"for");
            var open_arr = new Regex(@"\[\|");
            var close_arr = new Regex(@"\|\]");
            var arr_sel = new Regex(@"\.\[");
            var quid = new Regex(@"\.\D+");
            var tok = new Regex[] 
                {
                    mutable,
                    unit,
                    assign_m,
                    assign_r,
                    //do_,
                    while_,
                    for_,
                    open_arr,
                    close_arr,
                    arr_sel,
                    quid
                };
            return Search(tok, file);
        }
    }
}
